package com.jl.basicframework.manager;

import android.app.Activity;

import java.util.Stack;

/**
 * Created by 姜澜 on 2019/7/12.
 * Activity 的栈管理
 * 使用Stack栈
 */

public class ActivityManager {
    private static volatile ActivityManager activityManager;
    private Stack<Activity> mActivities;
    private ActivityManager(){
        mActivities = new Stack<>();
    }

    /**
     * 单例，双重锁
     * @return
     */
    public static ActivityManager getActivityManager(){
        if (activityManager==null){
            synchronized (ActivityManager.class){
                if (activityManager==null){
                    activityManager = new ActivityManager();
                }
            }
        }
        return activityManager;
    }

    /**
     * 统一管理
     * @param activity
     */
    public void attach(Activity activity){
        mActivities.add(activity);
    }

    /**
     * 解绑，防止溢出
     * @param detachActivity
     */
    public void detach(Activity detachActivity){
        int size = mActivities.size();
        for (int i = 0;i< size;i++){
            Activity activity = mActivities.get(i);
            if (activity== detachActivity){
                mActivities.remove(i);
                i--;
                size--;
            }
        }
    }

    /**
     * 关闭当前Activity
     * @param finishActivity
     */
    public void finish(Activity finishActivity){
        int size = mActivities.size();
        for (int i = 0;i< size;i++){
            Activity activity = mActivities.get(i);
            if (activity== finishActivity){
                mActivities.remove(i);
                i--;
                size--;
            }
        }
    }

    /**
     * 根据Activity的类名关闭Activity
     * @param activityClass
     */
    public void finish(Class<? extends Activity> activityClass){
        int size = mActivities.size();
        for (int i = 0;i< size;i++){
            Activity activity = mActivities.get(i);
            if (activity.getClass().getCanonicalName().equals(activityClass.getCanonicalName())){
                mActivities.remove(i);
                activity.finish();
                i--;
                size--;
            }
        }
    }

    /**
     * 获取当前的Activity
     * @return
     */
    public Activity currentActivity(){
        return mActivities.lastElement();
    }

    /**
     * 获取栈中指定Activity的上下文
     * @param activityClass
     * @return
     */
    public Activity getActivity(Class<? extends Activity> activityClass){
        int size = mActivities.size();
        for (int i = 0;i< size;i++){
            Activity activity = mActivities.get(i);
            if (activity.getClass().getCanonicalName().equals(activityClass.getCanonicalName())){
                return activity;
            }
        }
        return null;
    }

    /**
     * 将指定页面移动至栈顶
     * @param activityClass
     * @return
     */
    public Activity mobileActivityToTop(Class<? extends Activity> activityClass){

        int size = mActivities.size();
        for (int i = 0;i< size;i++){
            Activity activity = mActivities.get(0);
            if (!activity.getClass().getCanonicalName().equals(activityClass.getCanonicalName())){
                mActivities.remove(0);
                activity.finish();
                i--;
                size--;
            }else {
                //发现一致则直接跳出循环
                break;
            }
        }

        return currentActivity();
    }

    /**
     * 获取当前管理栈内activity的数量
     */
    public int getActivitySize(){
        return mActivities.size();
    }
}
